package com.hxzy.research.controller.usermanager;

import com.hxzy.research.pojo.TblCourse;
import com.hxzy.research.service.pageandmanager.AddCourseManager;
import com.hxzy.research.service.pageandmanager.FrontTeach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CourseController {
    @Autowired private FrontTeach frontTeach;
    @Autowired private AddCourseManager addCourseManager;

    /**
     *分公司课程
     */
    @RequestMapping("/user/manager/forwardCourseManager.go")
    public String forwardCourseManager(Model model,Integer branchid, HttpSession session)throws Exception{
        session.setAttribute("branchid",branchid);


        //查询课程
        List<TblCourse> courseList=frontTeach.findCourseByid(branchid);
        model.addAttribute("courseList",courseList);
        return "admin/manager/course/courseManager";
    }

    //跳转到课程添加页面
    @RequestMapping("/user/manager/forwardAddCourse.go")
    public String forwardAddCourse(HttpSession session,Integer branchid)throws Exception{

        return "admin/manager/course/addCourse";
    }
    //课程添加
    @RequestMapping("/user/manager/addCourse.go")
    public @ResponseBody TblCourse addCourse(TblCourse tblCourse,HttpSession session)throws Exception{

        //执行添加
        TblCourse tblCour=new TblCourse();
        tblCour.setBranchid((Integer) session.getAttribute("branchid"));
        tblCour.setCoursename(tblCourse.getCoursename());
        addCourseManager.addCourse(tblCour);
        return tblCourse;
    }

    //跳转到课程修改页面
    @RequestMapping("/user/manager/forwardUpdateCourse.go")
    public String forwardUpdateCourse(Model model,Integer kid,HttpSession session)throws Exception{
        session.setAttribute("kid",kid);
        //通过课程id查课程
        TblCourse course=addCourseManager.findCourseById(kid);
        model.addAttribute("course",course);
        return "admin/manager/course/updateCourse";
    }
    //课程修改
    @RequestMapping("/user/manager/updateCourse.go")
    public @ResponseBody TblCourse updateCourse(TblCourse tblCourse,HttpSession session)throws Exception{
        TblCourse tblCour=new TblCourse();
        //执行修改
        tblCour.setId((Integer) session.getAttribute("kid"));
        tblCour.setCoursename(tblCourse.getCoursename());
        tblCour.setBranchid(tblCourse.getBranchid());
        addCourseManager.updateCourse(tblCour);
        return tblCourse;
    }

    //课程的删除操作
    @RequestMapping("/user/manager/deleteCourseByKid.go")
    public String deleteCourseByKid(Integer kid,HttpSession session)throws Exception{
        //执行删除
        addCourseManager.deleteCourse(kid);
        Integer branchid=(Integer) session.getAttribute("branchid");
        return "redirect:/user/manager/forwardCourseManager.go?branchid="+branchid;
    }
}
